import 'package:flutter/material.dart';
import 'package:toast/toast.dart';

class PrefixContanier extends StatelessWidget {
  final String label;

  PrefixContanier(this.label, {Key key});

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50,
      child: Text(
        label,
        style: TextStyle(
          height: 2.4,
          color: Colors.black,
          fontSize: 16,
          letterSpacing: 1,
        ),
      ),
    );
  }
}

class FormText extends StatefulWidget {
  final Function validator;
  final Function onSave;
  final String hintText;
  final String initValue;
  final bool obscureText;
  final bool enabled;

  FormText(
      {this.initValue,
      this.validator,
      this.onSave,
      this.hintText,
      this.obscureText = false,
      this.enabled = true,
      Key key});
  @override
  _FormTextState createState() => _FormTextState();
}

class _FormTextState extends State<FormText> {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: 200,
      child: TextFormField(
        keyboardType: TextInputType.multiline,
        maxLines: null,
        obscureText: widget.obscureText,
        initialValue: widget.initValue,
        cursorHeight: 30,
        style: TextStyle(
          fontSize: 16,
          letterSpacing: 1,
        ),
        decoration: InputDecoration(
          hintText: widget.hintText,
        ),
        // ignore: missing_return
        validator: widget.validator,
        onSaved: widget.onSave,
        enabled: widget.enabled,
      ),
    );
  }
}

class FormInput extends StatefulWidget {
  final Function validator;
  final Function onSave;
  final String hintText;
  final String initValue;
  final bool obscureText;
  final bool enabled;

  FormInput(
      {this.initValue,
      this.validator,
      this.onSave,
      this.hintText,
      this.obscureText = false,
      this.enabled = true,
      Key key});

  @override
  _FormInputState createState() => _FormInputState();
}

class _FormInputState extends State<FormInput> {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: 200,
      child: TextFormField(
        obscureText: widget.obscureText,
        initialValue: widget.initValue,
        cursorHeight: 30,
        style: TextStyle(
          fontSize: 16,
          letterSpacing: 1,
        ),
        decoration: InputDecoration(
          hintText: widget.hintText,
        ),
        // ignore: missing_return
        validator: widget.validator,
        onSaved: widget.onSave,
        enabled: widget.enabled,
      ),
    );
  }
}

class FormOneSelect extends StatefulWidget {
  final String label;
  final List<Map<String, dynamic>> list;
  final Function select;

  FormOneSelect({this.label, this.list, this.select, Key key});

  @override
  _FormOneSelectState createState() => _FormOneSelectState();
}

class _FormOneSelectState extends State<FormOneSelect> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

showOneSelect() {}

showMulSelect(
  BuildContext context, {
  List<Map<String, dynamic>> parentList,
  List<Map<String, dynamic>> selectList,
}) async {
  if (parentList == null) {
    parentList = [];
  }
  if (selectList == null) {
    selectList = [];
  }
  List<bool> boolList = buildBoolList("key", selectList, parentList);
  Widget bulidListItem(BuildContext context, int index, StateSetter setState) {
    return CheckboxListTile(
      title: new Text(parentList[index]["value"].toString()),
      value: boolList[index],
      onChanged: (bool flag) {
        setState(() {
          boolList[index] = flag;
        });
      },
    );
  }

  List<int> buildIntList() {
    List<int> intList = [];
    for (int index = 0; index < boolList.length; index++) {
      if (boolList[index]) {
        intList.add(parentList[index]["key"]);
      }
    }
    return intList;
  }

  return showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          titlePadding: EdgeInsets.all(0),
          title: Container(
            height: 50,
            alignment: Alignment.center,
            child: Text(
              '多选框title',
              style: TextStyle(color: Colors.white),
            ),
            decoration: BoxDecoration(
              color: Colors.blue,
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(3),
                topRight: Radius.circular(3),
              ),
            ),
          ),
          contentPadding: EdgeInsets.all(0),
          content: new StatefulBuilder(
            builder: (context, StateSetter setState) {
              return Container(
                height: 300,
                width: 300,
                decoration: BoxDecoration(
                  color: Color.fromRGBO(250, 250, 250, 1),
                  borderRadius: BorderRadius.circular(10),
                ),
                child: Container(
                  margin: EdgeInsets.only(
                    top: 20,
                    bottom: 20,
                  ),
                  height: 260,
                  child: ListView.builder(
                      itemCount: parentList.length,
                      itemBuilder: (
                        context,
                        index,
                      ) {
                        return bulidListItem(context, index, setState);
                      }),
                ),
              );
            },
          ),
          actions: <Widget>[
            RaisedButton(
              color: Color.fromRGBO(33, 150, 243, 1),
              child: Text("保 存"),
              onPressed: () {
                Navigator.of(context).pop(buildIntList());
              },
            ),
            RaisedButton(
              color: Color.fromRGBO(245, 108, 108, 1),
              child: Text('重 置'),
              onPressed: () {
                print('重 置');
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      }).then((value) {
    return value;
  });
}

// 构建权限选项列表
List<bool> buildBoolList(String key, List<Map<String, dynamic>> sonList,
    List<Map<String, dynamic>> parentList) {
  List<bool> boolList = [];
  if (sonList == null || sonList.length == 0) {
    for (int index = 0; index < parentList.length; index++) {
      boolList.add(false);
    }
  } else {
    List<int> indexList = [];
    for (int sonIndex = 0; sonIndex < sonList.length; sonIndex++) {
      int index = itemIndexOf(sonList[sonIndex], parentList);
      if (index > -1) {
        indexList.add(index);
      }
    }
    for (int index = 0; index < parentList.length; index++) {
      if (indexList.indexOf(index) > -1) {
        boolList.add(true);
      } else {
        boolList.add(false);
      }
    }
  }
  return boolList;
}

int itemIndexOf(Map<String, dynamic> item, List<Map<String, dynamic>> list) {
  for (int index = 0; index < list.length; index++) {
    if (item["key"] == list[index]["key"]) {
      return index;
    }
  }
  return -1;
}

class FormMulSelect extends StatefulWidget {
  @override
  _FormMulSelectState createState() => _FormMulSelectState();
}

class _FormMulSelectState extends State<FormMulSelect> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

class InfoItem extends StatelessWidget {
  final String label;

  final String dataStr;

  InfoItem(this.label, this.dataStr);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(2),
      child: Row(
        children: [
          Spacer(
            flex: 5,
          ),
          Container(
            width: 80,
            alignment: Alignment.center,
            child: Text(
              label,
              style: TextStyle(
                color: Color.fromRGBO(150, 150, 150, 1),
              ),
            ),
          ),
          Text(':'),
          Spacer(
            flex: 3,
          ),
          Container(
            width: 120,
            alignment: Alignment.centerLeft,
            child: Text(
              dataStr,
            ),
          ),
          Spacer(
            flex: 5,
          ),
        ],
      ),
    );
  }
}

class InfoListItem extends StatelessWidget {
  final String label;

  final List<String> strList;

  InfoListItem(this.label, this.strList);

  Widget buildItem(int index) {
    return Text(strList[index]);
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Spacer(
          flex: 5,
        ),
        Container(
          width: 80,
          height: 200,
          alignment: Alignment.center,
          child: ListView(
            children: [
              Container(
                alignment: Alignment.center,
                child: Text(
                  label,
                  style: TextStyle(
                    color: Color.fromRGBO(150, 150, 150, 1),
                  ),
                ),
              ),
            ],
          ),
        ),
        Container(
          width: 5,
          height: 200,
          alignment: Alignment.topCenter,
          child: ListView(
            children: [
              Text(':'),
            ],
          ),
        ),
        Spacer(
          flex: 3,
        ),
        Container(
          width: 120,
          height: 200,
          alignment: Alignment.center,
          child: ListView.builder(
            itemCount: strList.length,
            itemBuilder: (BuildContext context, int index) {
              return buildItem(index);
            },
          ),
        ),
        Spacer(
          flex: 5,
        ),
      ],
    );
  }
}

class ItemTitle extends StatelessWidget {
  final String title;

  ItemTitle(this.title, {Key key});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(10),
      child: Row(
        children: [
          Container(
            child: Text(
              title,
              style: TextStyle(fontSize: 20),
            ),
          ),
        ],
      ),
    );
  }
}

class ItemContext extends StatelessWidget {
  final String label;
  final String data;
  final Color dataColor;

  ItemContext(this.label, this.data, {this.dataColor, Key key});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(left: 10, bottom: 10),
      child: Row(
        children: [
          Container(
            child: Text(
              label,
              style: TextStyle(color: Color.fromRGBO(150, 150, 150, 1)),
            ),
          ),
          SizedBox(
            width: 10,
          ),
          Container(
            width: 250,
            child: Text(
              data,
              style: TextStyle(color: dataColor),
            ),
          ),
        ],
      ),
    );
  }
}

class ItemField extends StatelessWidget {
  final String label;
  final String data;
  final Color dataColor;

  ItemField(this.label, this.data, {this.dataColor, Key key});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(left: 10, bottom: 10),
      child: Row(
        children: [
          Container(
            child: Text(
              label,
              style: TextStyle(color: Color.fromRGBO(150, 150, 150, 1)),
            ),
          ),
          SizedBox(
            width: 10,
          ),
          Container(
            child: TextField(
              maxLines: 8,
              style: TextStyle(color: dataColor),
              decoration: InputDecoration(labelText: data),
            ),
          ),
        ],
      ),
    );
  }
}

class ItemList extends StatelessWidget {
  final String label;

  final List<String> strList;

  ItemList(this.label, this.strList);

  Widget buildItem(int index) {
    return Text(strList[index]);
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Container(
          width: 80,
          height: 200,
          alignment: Alignment.center,
          child: ListView(
            children: [
              Container(
                alignment: Alignment.center,
                child: Text(
                  label,
                  style: TextStyle(
                    color: Color.fromRGBO(150, 150, 150, 1),
                  ),
                ),
              ),
            ],
          ),
        ),
        Container(
          width: 120,
          height: 200,
          alignment: Alignment.center,
          child: ListView.builder(
            itemCount: strList.length,
            itemBuilder: (BuildContext context, int index) {
              return buildItem(index);
            },
          ),
        ),
      ],
    );
  }
}

showMessage(BuildContext context, String message) {
  Toast.show(
    message,
    context,
    gravity: Toast.CENTER,
    duration: 2,
  );
}

showSuccessMessage(BuildContext context) {
  Toast.show(
    "操作成功",
    context,
    gravity: Toast.CENTER,
    duration: 2,
  );
}

showFailMessage(BuildContext context) {
  Toast.show(
    "操作失败",
    context,
    gravity: Toast.CENTER,
    duration: 2,
  );
}
